package cn.usian.service.Impl;

import cn.usian.common.R;
import cn.usian.dao.CategoryDao;
import cn.usian.dao.DishDao;
import cn.usian.dao.SetmealDao;
import cn.usian.entity.Category;
import cn.usian.entity.Dish;
import cn.usian.entity.Setmeal;
import cn.usian.exception.CustomerException;
import cn.usian.exception.NameException;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private SetmealDao setmealDao;
    @Resource
    private DishDao dishDao;

    @Override
    public R findPage(int page, int pageSize) {
        //创建page对象
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //添加排序的条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        Page<Category> categoryPage = categoryDao.selectPage(pageInfo, queryWrapper);
        return R.success(categoryPage);
    }

    @Override
    public void save(Category category) {
        //判断分类名是否存在
        if (findBynameName(category.getName()) != null) {
            throw new NameException(category.getName());
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryDao.insert(category);
    }
    @Override
    public Category findBynameName(String name) {
        LambdaQueryWrapper<Category> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,name);
        return categoryDao.selectOne(queryWrapper);
    }

    @Override
    public void updateEmp(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        if (category.getName()!=null&&!category.equals("")){
            updateWrapper.set(Category::getName,category.getName());
        }
        if (category.getType()!=null&&!category.equals("")){
            updateWrapper.set(Category::getType,category.getType());
        }
        if (category.getSort()!=null&&!category.equals("")){
            updateWrapper.set(Category::getSort,category.getSort());
        }
        updateWrapper.eq(Category::getId,category.getId());
        categoryDao.update(category,updateWrapper);
    }

    @Override
    public void del(long id) {
        LambdaQueryWrapper<Dish> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        List<Dish> dishList  = dishDao.selectList(queryWrapper);
        if (!dishList.isEmpty()){
            throw new CustomerException("该分类已经关联其他菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmealList = setmealDao.selectList(queryWrapper1);
        if(!setmealList.isEmpty()){
            throw new CustomerException("该分类已经关联其他菜品，不能删除");
        }
        categoryDao.deleteById(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.selectList(null);
    }
}
